import EventManager from "../../../Common/EventManager";
import { EventType } from "../../../GameSpecial/GameEventType";
import timePlugIn from "../../../ipha_tool/TimePlugIn";
import gameConfig from "../../config/gameConfig";
import UserService from "../../services/userService";
import { mgrs, sers } from "../../system/OverallSystem ";
import PageBase from "./pageBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GetPowerPage extends PageBase {

    public pageName: string = "获取体力";

    @property(cc.Label)
    lbRePowerCnt:cc.Label = null

    @property(cc.Label)
    number:cc.Label = null

    onLoad() {

    }

    start() {
        // let text = cc.find("layout/text", this.showNode)?.getComponent(cc.Label);
        // if (text) {
        //     text.string = "X " + gameConfig.VIDEO_GET_POWER;
        // }
        this.node.zIndex = 9999
        this.updateVideoCnt()
        this._init();
    }

    protected _init(): void {

        this.open();
    }

    OnBtnConfirm() {
        mgrs().soundMgr.playBtnSound();

        if(sers().userSrv.power>=gameConfig.MAX_POWER){
            sers().uiSrv.showMessage("体力已满");
            return
        }

        sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
            sers().userSrv.addPower(gameConfig.VIDEO_GET_POWER);
            sers().uiSrv.showMessage("获得体力" + gameConfig.VIDEO_GET_POWER + "点");
            // this.close();
        }, (err) => {

        }, null, {
            scene: '获得体力',
            event: '获得体力',
          },'common_video');
    }

    jinbigoumai(){
        mgrs().soundMgr.playBtnSound();

        if(sers().userSrv.power>=gameConfig.MAX_POWER){
            sers().uiSrv.showMessage("体力已满");
            return
        }

        if(sers().userSrv.coin<200){
            sers().uiSrv.showMessage(`金币不足`)
            return
        }
        sers().userSrv.addCoin(-200)
        sers().userSrv.addPower(gameConfig.VIDEO_GET_POWER);
        sers().uiSrv.showMessage("获得体力" + gameConfig.VIDEO_GET_POWER + "点");
        EventManager.emit(EventType.DirectorEvent.updataGold)
    }

    @property(cc.Node)
    changwanBtn:cc.Node = null

    @property(cc.Label)
    tips:cc.Label = null

    @property(cc.Label)
    tips2:cc.Label = null

    @property(cc.Label)
    wuxiandaojishi:cc.Label = null

    private static LookCnt = 0

    OnBtnEnterInifinityMod() {
        mgrs().soundMgr.playBtnSound();
        let changwanModel = Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0;
        if (changwanModel) {
            //XXX 
            sers().uiSrv.showMessage("已经开启畅玩模式！");
            // this.close();
            this.changwanBtn.active = false
        } else {
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                window['LookCnt']++;
                if (window['LookCnt'] >= 3) {
                    // sers().userSrv.addPower(999);
                    sers().userSrv.addPower(1999);
                    sers().localStorageSrv.set(UserService.infinityPowerModel, 1);
                    sers().localStorageSrv.set(UserService.infinityPowerDate, Date.now());
                    sers().localStorageSrv.set(UserService.changwanModel, 1);
                    sers().localStorageSrv.set(UserService.changwanModelDate, Date.now());
                    // this.close();
                    EventManager.emit(EventType.UIEvent.updataGamelist)
                }
                this.updateVideoCnt();
            }, () => {

            }, null, {
                scene: '24小时无限体力畅玩',
                event: '24小时无限体力畅玩',
              },'common_video');
        }
    }

    updateVideoCnt(){
        this.tips.string = `（${window['LookCnt']}/3）`
        this.changwanBtn.active = Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) == 1 ? false : true
        this.tips2.node.active = this.changwanBtn.active
        this.wuxiandaojishi.node.active = !this.changwanBtn.active
    }


    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        this.onlyClose();
    }

    @property(cc.Node)
    tilitubiaoNode:cc.Node = null

    update (dt) {

        if(Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0){
            this.tilitubiaoNode.children[1].active=false
            this.tilitubiaoNode.children[2].active=false
            this.tilitubiaoNode.children[3].active=true
            const date = Date.now();
            let preDate = sers().localStorageSrv.get(UserService.infinityPowerDate);
            let isInfinityPowerModel = Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0;
            let isDateOut24 = date - Number(preDate) >= 86400 * 1e3;

            // cc.log(86400 * 1e3- (date - Number(preDate)))
            this.wuxiandaojishi.string = '剩余时间:'+timePlugIn().numberShiftTime(Math.floor((86400 * 1e3- (date - Number(preDate)))/1000)) + ''
            if (preDate != null && preDate != "" && isInfinityPowerModel && isDateOut24) {
                //如果无限体力模式大于24小时
                sers().userSrv.setPower(gameConfig.MAX_POWER);
                UserService.unlockCnt = 0
                sers().localStorageSrv.set(UserService.infinityPowerModel, 0);
                sers().localStorageSrv.set(UserService.infinityPowerDate, date);
                window['LookCnt'] = 0
                this.updateVideoCnt()
            }
            return
        }

        this.tilitubiaoNode.children[1].active=true
        this.tilitubiaoNode.children[2].active=true
        this.tilitubiaoNode.children[3].active=false

        if (sers().userSrv.isPowerLess()) {
            this.lbRePowerCnt.node.active = true;
            const timer = sers().userSrv.getRePowerTimer();
            let time = Math.trunc(timer / 60) + ":" + Math.trunc(timer % 60);
            this.lbRePowerCnt.string = time;
            this.number.string = '(' +sers().userSrv.power+'/'+gameConfig.MAX_POWER + ')'
        } else {
            // this.lbRePowerCnt.node.active = false;
            this.lbRePowerCnt.string = '体力已满'
            this.number.string = '(' +sers().userSrv.power+'/'+gameConfig.MAX_POWER + ')'
            
        }
    }
}
